package com.giaothong.ui;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.giaothong.R;
import com.giaothong.config.Define;
import com.giaothong.config.Define.StoreAcount;
import com.giaothong.controller.AuthenticationController;
import com.giaothong.http.core.HttpAsyncCallback;
import com.giaothong.http.core.HttpConnectionId;
import com.giaothong.http.core.HttpResult;
import com.giaothong.model.ModelLoginRegisterRequest;
import com.giaothong.ui.base.BaseActivity;
import com.giaothong.utils.DialogUtil;
import com.giaothong.utils.SessionData;

public class RegisterActivity extends BaseActivity implements HttpAsyncCallback<JSONObject> {

	private EditText edtUsernameId, edtPassword, edtName, edtAddress, edtEmail, edtPhone, edtRePassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_register);
		init();
	}

	/**
	 * init View Id
	 */
	private void init() {
		edtUsernameId = (EditText) findViewById(R.id.edtUsernameId);
		edtPassword = (EditText) findViewById(R.id.edtPassword);
		edtName = (EditText) findViewById(R.id.edtName);
		edtAddress = (EditText) findViewById(R.id.edtAddress);
		edtEmail = (EditText) findViewById(R.id.edtEmail);
		edtPhone = (EditText) findViewById(R.id.edtPhone);
		edtRePassword = (EditText) findViewById(R.id.edtRePassword);
		((TextView) findViewById(R.id.txtTitleBar)).setText(R.string.register);
		findViewById(R.id.btnNext).setVisibility(View.INVISIBLE);
	}

	/*****************************
	 * onClick method
	 *****************************/
	/**
	 * onClick register
	 * 
	 * @param view
	 */
	public void onClickRegister(View view) {
		if (!edtUsernameId.getText().toString().equals("") && !edtPassword.getText().toString().equals("") && !edtName.getText().toString().equals("") && !edtAddress.getText().toString().equals("") && !edtEmail.getText().toString().equals("") && !edtPhone.getText().toString().equals("")) {
			if (edtPassword.getText().toString().equals(edtRePassword.getText().toString())) {
				AuthenticationController.registerRequest(this, this, edtUsernameId.getText().toString(), edtPassword.getText().toString(), edtName.getText().toString(), edtAddress.getText().toString(), edtEmail.getText().toString(), edtPhone.getText().toString());
			} else {
				DialogUtil.showDialog(this, R.string.error, R.string.password_not_correct);
			}
		} else {
			DialogUtil.showDialog(this, R.string.error, R.string.you_could_insert_all);
       }
	}

	/**
	 * call this activity
	 * 
	 * @param context
	 */
	public static void show(Context context) {
		Intent intent = new Intent(context, RegisterActivity.class);
		context.startActivity(intent);
	}

	/**
	 * process response from server
	 */
	public void onTaskComplete(HttpConnectionId requestCode, HttpResult resultCode, JSONObject result) {
		DialogUtil.showErrorDialogIfRequestFailed(this, resultCode);
		if (resultCode == HttpResult.SUCCESS) {
			switch (requestCode) {

			// response from server
			case REGISTER_REQUEST: {
				ModelLoginRegisterRequest modelLoginRegisterRequest = new ModelLoginRegisterRequest(result);
				if (modelLoginRegisterRequest.getCode() == Define.CODE_SUCCESSFULL) {
					
					// store user infor
					SessionData.setUserId(modelLoginRegisterRequest.getUserEntity().getUserId());
					SessionData.setUserType(modelLoginRegisterRequest.getUserEntity().getType());
					sharePreferenceAdapter.save(StoreAcount.USER_NAME, modelLoginRegisterRequest.getUserEntity().getUserName());
					sharePreferenceAdapter.save(StoreAcount.PASSWORD, edtPassword.getText().toString());
					MainMapActivity.show(RegisterActivity.this);
				} else {
					DialogUtil.showDialog(RegisterActivity.this, R.string.error, R.string.error_register);
				}
				break;
			}

			// default
			default: {
				// do nothing
				break;
			}
			} // end switch
		}
	}
}
